<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="../js/plugins/jrender/jrender.min.js"></script>
		<script src="../js/plugins/jquery-form/jquery.form.js"></script>

		<script>
            $(function () {
                //从静态页面取出用户之前存的数据,是一个字符串
                var userStr = sessionStorage.getItem("user");
                //将字符串解析成为一个json对象
                var user = JSON.parse(userStr);

                //console.log(user);
                //渲染数据,记得引进jrender
                $(".info").renderValues(user);

                //设置背景图片的样式,这里的是设置一个小样式用css
				//如果是大的样式是用addClass
				$(".myBg").css("background-image","url('"+user.coverImgUrl+"')");

                var flag=1;
				$("#headBtn").click(function () {
					flag=1;
				    //如果click里面的参数为空表示调用绑定在点击事件的函数
					$("#inputBtn").click();
                });


				//点击设置背景弹出文件上传框
				$("#coverBtn").click(function () {
				    flag=0;
                    $("#inputBtn").click();
                });

				//通过值改变事件来确认是否有文件上传
				$("#inputBtn").change(function () {
                    var value = this.value;
                    if (value) {
                        $("#imageForm").ajaxSubmit(function (data) {
                            if (data.status) {
								if (flag==1){
                                    $("#headImg").attr("src",data.url);
                                    //文件上传成功之后有值,给表单设置路径
									$("#headImgUrl").val(data.url);
								}else{
                                    $(".myBg").css("background-image","url('"+data.url+"')");
                                    //文件上传成功之后有值,给表单设置路径
                                    $("#coverImgUrl").val(data.url);
								}
                            }
                        });
                    }
                });

                $("#saveBtn").click(function () {
                    $("#infoForm").ajaxSubmit(function (data) {
                        if (data.success) {
                            //从后台返回的最新的数据,设置给静态页面
                            sessionStorage.setItem("user",JSON.stringify(data.data));

                            window.parent.location.href="/mine/profiles.html";
                        }
                    })
                })
            });


		</script>


	</head>

	<body>
		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="#" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>
		<div>
			<!--文件上传的表单-->
			<form method="post" action="/images" id="imageForm" enctype="multipart/form-data">
				<!--要注意的是这里的name属性要和ImageController里面的MultipartFile的形参名一样-->
				<!--样式的display:none表示隐藏下面这个表单控件-->
				<input type="file" name="pic" style="display: none;" id="inputBtn">


			</form>
		</div>
		<div class="container info">
			<div class="myBg">
				<img id="headImg" render-src="headImgUrl" name="headImgUrl" class="rounded-circle"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn" render-src="coverImgUrl" >设置背景</button>
			</div>

			<form id="infoForm" method="post" action="/users">
					<input type="hidden" name="id" render-value="id">
					<!--下面的这个配置是将请求的方式改变为post-->
					<input type="hidden" name="_method" value="PUT">
					<input type="hidden" name="headImgUrl" id="headImgUrl">
					<input type="hidden" name="coverImgUrl" id="coverImgUrl">
					<label>昵称</label><input type="text" name="nickName" render-value="nickName" class="form-control" >
					<label>性别</label><select class="form-control" name="gender" render-value="gender" >
							<option value="-1">保密</option>
							<option value="0">男</option>
							<option value="1">女</option>
						</select>
					<label>地区</label><input type="text" render-value="place" name="place" class="form-control">
					<label>签名</label><textarea name="sign" render-value="sign" class="form-control" rows="5" ></textarea>
			</form>
		</div>
	</body>
</html>
